#include "Menu.h"

uint8_t fontnian[][16] ={
/*-- ID:0,字符:"年",ASCII编码:C4EA,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
0x00,0x20,0x10,0xcc,0x47,0x44,0x44,0xfc,0x44,0x44,0x44,0x64,0x46,0x04,0x00,0x00,
0x04,0x04,0x04,0x07,0x04,0x04,0x04,0xff,0x04,0x04,0x04,0x04,0x04,0x06,0x04,0x00

};
uint8_t fontyue[][16] ={
		/*-- ID:0,字符:"月",ASCII编码:D4C2,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
0x00,0x00,0x00,0x00,0xfe,0x22,0x22,0x22,0x22,0x22,0x22,0xff,0x02,0x00,0x00,0x00,
0x00,0x80,0x40,0x30,0x0f,0x02,0x02,0x02,0x02,0x42,0x82,0x7f,0x00,0x00,0x00,0x00
};
uint8_t fontri[][16] ={
		/*-- ID:0,字符:"日",ASCII编码:C8D5,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
0x00,0x00,0x00,0xfe,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0xff,0x02,0x00,0x00,0x00,
0x00,0x00,0x00,0x7f,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x7f,0x00,0x00,0x00,0x00

};
uint8_t fontxingqi[][16] ={
/*-- ID:0,字符:"星",ASCII编码:D0C7,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
0x00,0x00,0xbe,0x2a,0x2a,0x2a,0x2a,0xea,0x2a,0x2a,0x2a,0x2a,0xbf,0x02,0x00,0x00,
0x48,0x44,0x4b,0x49,0x49,0x49,0x49,0x7f,0x49,0x49,0x49,0x4d,0x49,0x61,0x40,0x00,

/*-- ID:1,字符:"期",ASCII编码:C6DA,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
0x00,0x08,0xff,0xa8,0xa8,0xa8,0xff,0x08,0x00,0xfe,0x22,0x22,0x22,0xff,0x02,0x00,
0x84,0x44,0x37,0x04,0x04,0x14,0xa7,0x44,0x24,0x1f,0x02,0x42,0x82,0x7f,0x00,0x00

};
uint8_t fontIndex[][16] ={
/*-- ID:0,字符:"一",ASCII编码:D2BB,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xc0,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

/*-- ID:1,字符:"二",ASCII编码:B6FE,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
0x00,0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x0c,0x08,0x00,0x00,0x00,
0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x18,0x10,0x00,

/*-- ID:2,字符:"三",ASCII编码:C8FD,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
0x00,0x04,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0xc4,0x86,0x04,0x00,0x00,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x30,0x20,0x00,

/*-- ID:3,字符:"四",ASCII编码:CBC4,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
0x00,0xfc,0x04,0x04,0x04,0xfc,0x04,0x04,0x04,0xfc,0x04,0x04,0x04,0xfe,0x04,0x00,
0x00,0x7f,0x20,0x28,0x24,0x23,0x20,0x20,0x20,0x21,0x22,0x22,0x22,0x7f,0x00,0x00,

/*-- ID:4,字符:"五",ASCII编码:CEE5,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
0x00,0x04,0x84,0x84,0x84,0x84,0xfc,0x84,0x84,0x84,0x84,0xc4,0x86,0x84,0x00,0x00,
0x40,0x40,0x40,0x40,0x78,0x47,0x40,0x40,0x40,0x40,0x40,0x7f,0x40,0x60,0x40,0x00,

/*-- ID:5,字符:"六",ASCII编码:C1F9,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
0x20,0x20,0x20,0x20,0x20,0x22,0x2c,0x38,0x20,0x20,0x20,0x20,0x20,0x30,0x20,0x00,
0x00,0x40,0x20,0x18,0x06,0x03,0x00,0x00,0x00,0x01,0x02,0x0c,0x38,0x70,0x00,0x00,

/*-- ID:6,字符:"日",ASCII编码:C8D5,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
0x00,0x00,0x00,0xfe,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0xff,0x02,0x00,0x00,0x00,
0x00,0x00,0x00,0x7f,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x7f,0x00,0x00,0x00,0x00
};


// 16X16汉字
uint8_t font1[][16] = {
	0x10,0x22,0x64,0x0c,0x80,0x00,0xfe,0x92,0x92,0x92,0x92,0x92,0xff,0x02,0x00,0x00,
	0x04,0x04,0xfe,0x01,0x40,0x7e,0x42,0x42,0x7e,0x42,0x7e,0x42,0x42,0x7e,0x40,0x00,

	0x00,0x00,0xfc,0x24,0x24,0x24,0xfc,0xa5,0xa6,0xa4,0xfc,0x24,0x34,0x26,0x04,0x00,
	0x40,0x20,0x9f,0x80,0x42,0x42,0x26,0x2a,0x12,0x2a,0x26,0x42,0x40,0xc0,0x40,0x00,//"温度"
};
uint8_t font2[][16] = {
	0x10,0x22,0x64,0x0c,0x80,0xfe,0x92,0x92,0x92,0x92,0x92,0x92,0xff,0x02,0x00,0x00,
	0x04,0x04,0xfe,0x41,0x44,0x48,0x50,0x7f,0x40,0x40,0x7f,0x50,0x48,0x64,0x40,0x00,

	0x00,0x00,0xfc,0x24,0x24,0x24,0xfc,0xa5,0xa6,0xa4,0xfc,0x24,0x34,0x26,0x04,0x00,
	0x40,0x20,0x9f,0x80,0x42,0x42,0x26,0x2a,0x12,0x2a,0x26,0x42,0x40,0xc0,0x40,0x00,//"湿度"
};
uint8_t font3[][16] = {
	/*-- ID:0,字符:"度",ASCII编码:B6C8,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
	0x00,0x00,0xfc,0x24,0x24,0x24,0xfc,0xa5,0xa6,0xa4,0xfc,0x24,0x34,0x26,0x04,0x00,
	0x40,0x20,0x9f,0x80,0x42,0x42,0x26,0x2a,0x12,0x2a,0x26,0x42,0x40,0xc0,0x40,0x00
};
uint8_t font4[][16] ={
	/*-- ID:0,字符:"最",ASCII编码:D7EE,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
	0x40,0x40,0xc0,0x5f,0x55,0x55,0xd5,0x55,0x55,0x55,0x55,0x5f,0x40,0x60,0x40,0x00,
	0x20,0x20,0x3f,0x15,0x15,0x15,0xff,0x81,0x47,0x29,0x11,0x29,0x65,0xc3,0x40,0x00,

	/*-- ID:1,字符:"大",ASCII编码:B4F3,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
	0x20,0x20,0x20,0x20,0x20,0x20,0xa0,0x7f,0xa0,0x20,0x20,0x20,0x20,0x30,0x20,0x00,
	0x00,0x40,0x40,0x20,0x10,0x0c,0x03,0x00,0x01,0x06,0x08,0x10,0x20,0x60,0x20,0x00
};
uint8_t font5[][16] ={
		/*-- ID:0,字符:"最",ASCII编码:D7EE,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
	0x40,0x40,0xc0,0x5f,0x55,0x55,0xd5,0x55,0x55,0x55,0x55,0x5f,0x40,0x60,0x40,0x00,
	0x20,0x20,0x3f,0x15,0x15,0x15,0xff,0x81,0x47,0x29,0x11,0x29,0x65,0xc3,0x40,0x00,

	/*-- ID:1,字符:"小",ASCII编码:D0A1,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
	0x00,0x00,0x00,0x00,0x80,0x60,0x00,0xff,0x00,0x20,0x40,0x80,0x00,0x00,0x00,0x00,
	0x00,0x08,0x04,0x02,0x01,0x40,0x80,0x7f,0x00,0x00,0x00,0x00,0x01,0x06,0x00,0x00
};

//uint8_t flag = 0;
//uint8_t MaxTemperature = 30;
//uint8_t MinTemperature = 20;
//uint8_t MaxHumidity = 70;
//uint8_t MinHumidity = 40;
//uint8_t tmp,count = 0;

uint8_t flag = 0;
uint32_t MaxTemperature = 30;
uint32_t MinTemperature = 20;
uint32_t MaxHumidity = 70;
uint32_t MinHumidity = 40;
uint8_t tmp,count = 0;



void OLED_ClearFlag(void)
{
	for(uint8_t i = 0; i < 8; i++)
	{
		for(uint8_t j = 0; j < 8; j++)
		{
			_setPosition(i, j);
			_writeDATA(0x00);
		}
	}
}
void OLED_ShowFont()
{
	OLED_ShowNum8X16(0,0,20,2);
	OLED_ShowChar8X16(2,16,':');
	OLED_ShowChar8X16(2,40,':');
	OLED_ShowChinese16X16(0,32,0,fontnian);
	OLED_ShowChinese16X16(0,64,0,fontyue);
	OLED_ShowChinese16X16(0,94,0,fontri);
	OLED_ShowLongChinese16X16(2,72,2,fontxingqi);
	OLED_ShowChinese16X16(4,64,1,font1);
	OLED_ShowChar8X16(6,64,'%');
	
}
void Menu()
{
	
	
	OLED_Clear();
	OLED_ShowLongChinese16X16(0,0+8,2,font4);
	OLED_ShowLongChinese16X16(0,32+8,2,font1);
	OLED_ShowChar8X16(0,64+8,':');
	OLED_ShowNum8X16(0,64+16+8,MaxTemperature,2);
    OLED_ShowChinese16X16(0,96+8,0,font3);
	
	OLED_ShowLongChinese16X16(2,0+8,2,font5);
	OLED_ShowLongChinese16X16(2,32+8,2,font1);
	OLED_ShowChar8X16(2,64+8,':');
	OLED_ShowNum8X16(2,64+16+8,MinTemperature,2);
	OLED_ShowChinese16X16(2,96+8,0,font3);
	
	OLED_ShowLongChinese16X16(4,0+8,2,font4);
	OLED_ShowLongChinese16X16(4,32+8,2,font2);
	OLED_ShowChar8X16(4,64+8,':');
	OLED_ShowNum8X16(4,64+16+8,MaxHumidity,2);
	OLED_ShowChar8X16(4,96+8,'%');
	
	OLED_ShowLongChinese16X16(6,0+8,2,font5);
	OLED_ShowLongChinese16X16(6,32+8,2,font2);
	OLED_ShowChar8X16(6,64+8,':');
	OLED_ShowNum8X16(6,64+16+8,MinHumidity,2);
	OLED_ShowChar8X16(6,96+8,'%');
	
	
	while(1)
	{
		
		if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0)
		{
			while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0);
			OLED_Clear();
			OLED_ShowFont();
			flag = 0;
			return ;
		}
		if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14)==0)
		{
			HAL_TIM_Encoder_Start(&htim2,TIM_CHANNEL_ALL);
			while(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14)==0);	
			
			if(count == 0)
			{
				OLED_ClearFlag();
				tmp = MaxTemperature;
				OLED_ShowChar8X16(0,0,'*');				
				int cnt = __HAL_TIM_GET_COUNTER(&htim2);
				while(1)
				{
					if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0)
					{
						while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0);
						OLED_Clear();
						OLED_ShowFont();
						flag = 0;
						return ;
					}	
					int prival = cnt;
					cnt = __HAL_TIM_GET_COUNTER(&htim2);
					if(prival < cnt)
					{
						
						tmp ++;
					}
					else if(prival > cnt)
					{
						tmp --;
					}
					
					OLED_ShowNum8X16(0,64+16+8,tmp,2);
					HAL_Delay(50);
					if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14)==0)
					{
						while(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14)==0);
	
						break;
					}
					if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_15)==0)
					{
						MaxTemperature = tmp;
						while(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_15)==0);
						Flash_Write_Num(MaxTemperature,0); // 写入flash
						OLED_ShowChar8X16(0,0,'#');
						break ;
					}
				}
				
			}
			else if(count == 1)
			{
				OLED_ClearFlag();
				tmp = MinTemperature;
				OLED_ShowChar8X16(2,0,'*');
				int cnt = __HAL_TIM_GET_COUNTER(&htim2);
				while(1)
				{
					if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0)
					{
						while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0);
						OLED_Clear();
						OLED_ShowFont();
						flag = 0;
						return ;
					}
					int prival = cnt;
					cnt = __HAL_TIM_GET_COUNTER(&htim2);
					if(prival < cnt)
					{
						
						tmp ++;
					}
					else if(prival > cnt)
					{
						tmp --;
					}
					
					OLED_ShowNum8X16(2,64+16+8,tmp,2);
					HAL_Delay(50);
					if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14)==0)
					{
						while(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14)==0);
						break;
					}
					if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_15)==0)
					{
						MinTemperature = tmp;
						while(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_15)==0);
						Flash_Write_Num(tmp,1); // 写入FLASH
						OLED_ShowChar8X16(2,0,'#');
						//OLED_Clear();
						break ;
					}
				}
				
				
			}
			else if(count == 2)
			{
				OLED_ClearFlag();
				tmp = MaxHumidity;
				OLED_ShowChar8X16(4,0,'*');
				
				int cnt = __HAL_TIM_GET_COUNTER(&htim2);
				while(1)
				{
					if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0)
					{
						while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0);
						OLED_Clear();
						OLED_ShowFont();
						flag = 0;
						return ;
					}	
					int prival = cnt;
					cnt = __HAL_TIM_GET_COUNTER(&htim2);
					if(prival < cnt)
					{
						
						tmp ++;
					}
					else if(prival > cnt)
					{
						tmp --;
					}
					
					OLED_ShowNum8X16(4,64+16+8,tmp,2);
					HAL_Delay(50);
					if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14)==0)
					{
						while(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14)==0);
						break;
					}
					if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_15)==0)
					{
						MaxHumidity = tmp;
						while(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_15)==0);
						Flash_Write_Num(tmp,2);
						OLED_ShowChar8X16(4,0,'#');
						break ;
					}
				}
				
			}
			else if(count == 3)
			{
				OLED_ClearFlag();
				tmp = MinHumidity;
				OLED_ShowChar8X16(6,0,'*');
				
				int cnt = __HAL_TIM_GET_COUNTER(&htim2);
				while(1)
				{
					if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0)
					{
						while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==0);
						OLED_Clear();
						OLED_ShowFont();
						flag = 0;
						return ;
					}	
					int prival = cnt;
					cnt = __HAL_TIM_GET_COUNTER(&htim2);
					if(prival < cnt)
					{
						
						tmp ++;
					}
					else if(prival > cnt)
					{
						tmp --;
					}
					
					OLED_ShowNum8X16(6,64+16+8,tmp,2);
					HAL_Delay(50);
					if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14)==0)
					{
						while(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14)==0);
						break;
					}
					if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_15)==0)
					{
						MinHumidity = tmp;
						while(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_15)==0);
						Flash_Write_Num(tmp,3);
						OLED_ShowChar8X16(6,0,'#');
						break ;
					}
				}
			}
			
			count++;
			if(count==4)
			{
				count = 0;
			}
		}
		
		//return ;
	}
	
}
